/*
 * @Author: WJ23000 624473119@qq.com
 * @Date: 2023-10-08 15:11:26
 * @LastEditors: WJ23000 624473119@qq.com
 * @LastEditTime: 2023-10-08 15:11:38
 * @FilePath: \ekee-web\src\utils\bigIntJson.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
export default class BigIntJsonService {
  static parse(jsonStr) {
    /*
    明确后端不会出现数组中嵌套数组的数据结构
    例如[1111111112222222,[11111111122222222]]不能存在
    只能匹配 key:number 与 key:[number]场景(其他场景不允许匹配特别是用户的自定义输入内容)
    "key":1111111111111
    "key":[11111111,222222222]
     */
    let str = jsonStr.replace(
      /("[^"\s\\]+")\s*:\s*(\d{15,})\s*(,?)/g,
      `$1:"$2"$3`
    );

    // eslint-disable-next-line
    str = str.replace(
      /("[^"\s]+")\s*:\s*(\[[^\[\]\{\}]+])\s*(,?)/g,
      function (string) {
        let str = string;
        str = str.replace(/,\s*(\d{15,})\s*]/g, `,"$1"]`);
        str = str.replace(/\[\s*(\d{15,})\s*,/g, `["$1",`);
        str = str.replace(/\[\s*(\d{15,})\s*]/g, `["$1"]`);
        str = str.replace(/(?<=,)\s*(\d{15,})\s*(?=,)/g, `"$1"`);
        return str;
      }
    );

    // eslint-disable-next-line
    str = str.replace(
      /("[^"\s]+")\s*:\s*("\{.*\}")\s*(,?)/g,
      function (string) {
        let str = string;
        str = str.replace(
          /(\\"[^"\s]+")\s*:\s*(\d{15,})\s*(,?)/g,
          `$1:\\"$2\\"$3`
        );
        return str;
      }
    );

    return JSON.parse(str);
  }
}
